﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_AdminCP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["AdminName"] != null)
            {
                if ((string)Session["Authority"] == "Admin")
                {
                    this.DisplayChanged();
                    this.DisplayNew();
                    this.DisplayUnpaid();
                    this.DisplayPaid();
                }
                else
                    Response.Redirect("SanPham.aspx");
            }
            else
                Response.Redirect("Default.aspx");
        }
    }

    public void DisplayNew()
    {
        string sql = string.Format(@"
                                        SELECT
                                            b.OrderID,
                                            a.FullName,
                                            a.SDT,
                                            b.NgayDat,
                                            b.NgayNhan,
                                            b.NoiNhan,
                                            b.TrangThai 
                                        FROM
                                            customers a INNER JOIN Orders b 
                                                        ON a.CustomerID = b.CustomerID,
                                            QuanTri qt
                                        WHERE b.NgayDat>qt.LastLog AND qt.ID=N'{0}'
                                        Order by b.NgayDat DESC
                                        ",Session["AdminName"].ToString());

        DataTable dtb = new DataTable();
        dtb = DataBase.GetDataTable(sql);
        if (dtb.Rows.Count > 0)
        {
            GridView1.DataSource = dtb;
            GridView1.DataBind();
            MultiView1.ActiveViewIndex = 1;
        }
        else
            MultiView1.ActiveViewIndex = 0;
    }

    public void DisplayChanged()
    {
        string sql = "SELECT * FROM ThayDoi";
        DataTable dtb = DataBase.GetDataTable(sql);
        if (dtb.Rows.Count > 0)
        {
            GridView2.DataSource = dtb;
            GridView2.DataBind();
            MultiView2.ActiveViewIndex = 1;
        }
        else
            MultiView2.ActiveViewIndex = 0;
    }

    public void DisplayUnpaid()
    {
        string sql = string.Format(@"SELECT
                                            b.OrderID,
                                            a.FullName,
                                            a.SDT,
                                            b.NgayDat,
                                            b.NgayNhan,
                                            b.NoiNhan,
                                            b.TrangThai 
                                        FROM
                                            customers a INNER JOIN Orders b 
                                                        ON a.CustomerID = b.CustomerID
                                        WHERE b.TrangThai=N'Đặt hàng'
                                        AND b.NgayNhan = '{0}'  
                                        ", DateTime.Now.Date);
        DataTable dtb = new DataTable();
        dtb = DataBase.GetDataTable(sql);
        GridView3.DataSource = dtb;
        GridView3.DataBind();
    }

    public void DisplayPaid()
    {
        string sql = string.Format(@"SELECT
                                            b.OrderID,
                                            a.FullName,
                                            a.SDT,
                                            b.NgayDat,
                                            b.NgayNhan,
                                            b.NoiNhan,
                                            b.TrangThai 
                                        FROM
                                            customers a INNER JOIN Orders b 
                                                        ON a.CustomerID = b.CustomerID
                                        WHERE b.TrangThai=N'Đã thanh toán'
                                        AND b.NgayNhan = '{0}'  
                                        ", DateTime.Now.Date);
        DataTable dtb = new DataTable();
        dtb = DataBase.GetDataTable(sql);
        GridView4.DataSource = dtb;
        GridView4.DataBind();
    }

    protected void btnConfirmNew_Click(object sender, EventArgs e)
    {
        String sqlupdate = "UPDATE QuanTri SET LastLog = @LastLog WHERE ID=@ID";
        DataBase.ExecuteNonQuery(sqlupdate, "@LastLog", DateTime.Now, "@ID", Session["AdminName"].ToString());
        this.DisplayNew();
    }

    protected void btnChecked_Click(object sender, EventArgs e)
    {
        string sql = " DELETE FROM ThayDoi WHERE OrderID LIKE '%' ";
        DataBase.ExecuteNonQuery(sql);
        string sql1 = "UPDATE QuanTri SET Modified=0 WHERE Quyen='Admin'";
        DataBase.ExecuteNonQuery(sql1);
        //Response.Redirect("AdminCP.aspx");
        this.DisplayChanged();
    }

    protected void btnConfirmPaid_Click(object sender, EventArgs e)
    {
        if (GridView3.Rows.Count > 0)
        {
            foreach (GridViewRow ViewRow in GridView3.Rows)
            {
                CheckBox cb = (CheckBox)ViewRow.Cells[0].FindControl("CheckBox1");
                if (cb.Checked == true)
                {
                    string orderid = ViewRow.Cells[1].Text;
                    string sqlorder = string.Format(@"UPDATE Orders SET TrangThai=N'Đã thanh toán'
                                          WHERE OrderID = {0}", orderid);
                    DataBase.ExecuteNonQuery(sqlorder);
                }
            }
            this.DisplayUnpaid();
            this.DisplayPaid();
        }
        else return;
    }

    protected void btnConfirmComplete_Click(object sender, EventArgs e)
    {
        if (GridView4.Rows.Count > 0)
        {
            foreach (GridViewRow ViewRow in GridView4.Rows)
            {
                CheckBox cb = (CheckBox)ViewRow.Cells[0].FindControl("CheckBox2");
                if (cb.Checked == true)
                {
                    string orderid = ViewRow.Cells[1].Text;
                    string sqlorder = string.Format(@"UPDATE Orders SET TrangThai=N'Hoàn thành'
                                          WHERE OrderID = {0}", orderid);
                    DataBase.ExecuteNonQuery(sqlorder);
                }
            }
            this.DisplayUnpaid();
            this.DisplayPaid();
        }
        else return;
    }
    
}
